package com.uroad.lib.util.string;

import android.text.TextUtils;

import com.uroad.lib.util.log.LogUtil;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;

/**
 * 字符串处理工作类
 */
public class StringUtils {

    private static final String TAG = "StringUtils";
    // URL中文编码
    public static String encoder(String url)
            throws UnsupportedEncodingException {
        // 转中文
        String url1 = url.substring(0, url.lastIndexOf('/') + 1);
        String envalue = url.substring(url.lastIndexOf('/') + 1);
        String str = url1 + URLEncoder.encode(envalue, "UTF-8");
        return str;
    }

    public static long Convert2Long(Object object) {
        try {
            return Long.parseLong(object.toString());
        } catch (Exception e) {
            LogUtil.e(TAG,"Convert2Long-->异常");
            return 0;
        }
    }

    public static double Convert2Double(Object object) {
        try {
            return Double.parseDouble(object.toString());
        } catch (Exception e) {
            LogUtil.e(TAG,"Convert2Double-->异常");
            return 0;
        }
    }

    public static int Convert2Int(Object obj) {
        try {
            return Integer.valueOf(obj.toString().trim());
        } catch (Exception e) {
            LogUtil.e(TAG,"Convert2Int-->异常");
            return 0;
        }
    }

    public static float Convert2Float(String val) {
        try {
            return Float.valueOf(val);
        } catch (Exception e) {
            LogUtil.e(TAG,"Convert2Float-->异常");
            return 0;
        }
    }

    public static String Double2String(double val) {
        try {
            return Double.toString(val);
        } catch (Exception e) {
            LogUtil.e(TAG,"Double2String-->异常");
            return "";
        }
    }

    /**
     * Float类型转String，并保留两位小数
     * 先将folat转为double，在将double转为String
     * 直接Float.toString的话会得到字符串 如下：1.2386756E7
     * 不够直观
     *
     * @author hrd
     * @date 2017/6/15 0015
     */
    public static String Float2StringAndKeepTwoDecimalPlaces(float val) {
        try {
            BigDecimal b = new BigDecimal(String.valueOf(val));
            double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return Double2String(d);
        } catch (Exception e) {
            LogUtil.e(TAG,"Float2StringAndKeepTwoDecimalPlaces-->异常");
            return "";
        }
    }

    /**
     * 除以100
     *
     * @param str
     * @return
     */
    public static String divideHundred(String str) {
        try {
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(Double.parseDouble(str) / 100.00);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /** 乘以100 */
    public static String multiplyHundred(String str) {
        try {
            String value = (long) (Double.parseDouble(str) * 100) + "";
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    public static DecimalFormat format = new DecimalFormat("0.00");
    /**
     * 解析字符串数字保留后两位小数
     * @param num string类型
     * @return
     */
    public static String formatTwoDecimalPlaces(String num) {
        try {
            if (!TextUtils.isEmpty(num)) {
                return format.format(Double.parseDouble(num));
            }else{
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    /**
     * 解析字符串数字保留后两位小数
     * @param num double类型
     * @return
     */
    public static String formatTwoDecimalPlacesDouble(double num) {
        try {
            return format.format(num);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}